import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'
import i18n from '@/lang'

/**
 * Note: sub-menu only appear when route children.length >= 1
   * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
 roles: ['admin','editor']    control the page roles (you can set multiple roles)
 title: 'title'               the name show in sidebar and breadcrumb (recommend set)
 icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
 breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
 activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
 }
 */

const word_is = (name) => i18n.messages[i18n.locale][name]

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */

export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true,
    meta: { title: '' }
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/app_management/index'
  },

  /*   {
    path: '/example',
    component: Layout,
    redirect: '/table/index',
    name: 'Example',
    meta: { title: 'example', icon: 'el-icon-s-help' },
    children: [
      {
        path: 'table',
        name: 'Table',
        component: () => import('@/views/table/index'),
        meta: { title: 'Table', icon: 'table' }
      },
      {
        path: 'tree',
        name: 'Tree',
        component: () => import('@/views/tree/index'),
        meta: { title: 'Tree', icon: 'tree' }
      }
    ]
  }, */

  // 应用管理
  {
    path: '/app_management',
    component: Layout,
    meta: { title: word_is('Project_Management'), icon: 'el-icon-menu', breadcrumb: false },
    redirect: '/app_management/index',
    name: 'App',
    children: [
      {
        path: 'index',
        name: 'AppIndex',
        component: () => import('@/views/app_management/index'),
        meta: { title: word_is('Project_Management'), icon: 'form', activeMenu: '/app_management' },
        hidden: true,
        children: [
          {
            path: 'new',
            component: () => import('@/views/app_management/new/index'),
            meta: { title: word_is('new') + word_is('application'), icon: 'dashboard' },
            hidden: true
          },
          {
            path: 'details',
            component: () => import('@/views/app_management/details/index.vue'),
            name: 'Detail',
            meta: { title: word_is('Container_Detail') },
            hidden: true,
            children: [
              {
                path: 'chatExecutingUser',
                component: () => import('@/views/app_management/details/executingUser.vue'),
                name: 'ChatExecutingUser',
                meta: { title: word_is('startover') },
                hidden: true
              },
              {
                path: 'chatExecutingDeveloper',
                component: () => import('@/views/app_management/details/chatExecutingDeveloper.vue'),
                name: 'ChatExecutingDeveloper',
                meta: { title: word_is('debugging') },
                hidden: true
              },
              {
                path: 'edit',
                component: () => import('@/views/app_management/new/index.vue'),
                meta: { title: word_is('edit_application'), icon: 'dashboard' },
                hidden: true
              }
            ]
          },
          {
            path: 'event-driven',
            component: () => import('@/views/app_management/event_driven/index'),
            meta: { title: '事件驱动配置' },
            hidden: true
          }
        ]
      }
    ]
  },

  // 方法管理
  {
    path: '/methodology',
    component: Layout,
    name: 'Methodology',
    meta: { title: word_is('Method_management'), icon: 'el-icon-s-management', breadcrumb: false },
    redirect: '/methodology/index',
    children: [
      {
        path: 'index',
        name: 'MethodologyIndex',
        component: () => import('@/views/methodology/index'),
        meta: { title: word_is('Method_management'), activeMenu: '/methodology' },
        hidden: true,
        children: [
          {
            path: 'create',
            component: () => import('@/views/methodology/newMeth/index.vue'),
            name: 'CreateMethodology',
            meta: { title: word_is('New_method') },
            hidden: true
          },
          {
            path: 'detail',
            component: () => import('@/views/methodology/detail/index.vue'),
            name: 'methodologyDetail',
            meta: { title: word_is('method') + word_is('detail') },
            hidden: true,
            children: [
              {
                path: 'edit',
                component: () => import('@/views/methodology/editMeth/index.vue'),
                meta: { title: word_is('Edit_method') },
                hidden: true
              }
            ]
          }
        ]
      }
    ]
  },

  // 工具管理
  {
    path: '/tool',
    component: Layout,
    name: 'Tool',
    redirect: '/tool/index',
    meta: { title: word_is('Tool_management'), icon: 'el-icon-s-cooperation', breadcrumb: false },
    children: [
      {
        path: 'index',
        name: 'ToolIndex',
        component: () => import('@/views/tool/index'),
        meta: { title: word_is('Tool_management'), activeMenu: '/tool' },
        hidden: true,
        children: [
          {
            path: 'edit',
            beforeEnter: (to, from, next) => {
              if (to.query.is_edit) {
                to.meta.title = word_is('Edit_tool')
              } else {
                to.meta.title = word_is('New_tool')
              }
              next()
            },
            component: () => import('@/views/tool/updateTool/index.vue'),
            name: 'UpdateTool',
            hidden: true
          },
          {
            path: 'detail',
            component: () => import('@/views/tool/detailTool/index.vue'),
            name: 'detailTool',
            meta: { title: word_is('tool') + word_is('detail') },
            hidden: true
          }
        ]
      }
    ]
  },

  // 用户管理
  // {
  //   path: '/user',
  //   component: Layout,
  //   name: 'user',
  //   meta: { title: word_is('User_Management'),
  //     icon: 'el-icon-document-copy',
  //     breadcrumb: false,
  //     roles: ['root', 'admin']
  //   },
  //   redirect: '/user/index',
  //   children: [
  //     {
  //       path: 'index',
  //       name: 'UserIndex',
  //       component: () => import('@/views/user/index'),
  //       meta: { title: word_is('User_Management'), activeMenu: '/user' }
  //       // hidden: true,
  //     },
  //     {
  //       path: 'userDetail/index',
  //       name: 'UserDetail',
  //       component: () => import('@/views/user/userDetail/index'),
  //       meta: { title: word_is('User_Management'), activeMenu: '/user/userDetail' },
  //       hidden: true
  //     },
  //     {
  //       path: 'userGroup',
  //       name: 'UserGroup',
  //       component: () => import('@/views/user/user_group_list/index'),
  //       meta: { title: word_is('User_Group'), activeMenu: '/user/user_group_list' }
  //       // hidden: true,
  //     }
  //   ]
  // },

  // 设置
  {
    path: '/configuration',
    component: Layout,
    redirect: 'noRedirect',
    name: 'Configuration',
    alwaysShow: true,
    meta: {
      title: word_is('setting'),
      icon: 'el-icon-s-operation',
      roles: ['root', 'admin']
    },
    children: [
      {
        path: 'user',
        name: 'UserIndex',
        component: () => import('@/views/configuration/user/index.vue'),
        meta: { title: word_is('User') },
        children: [
          {
            path: 'create',
            component: () => import('@/views/configuration/user/newUser/index.vue'),
            name: 'CreateUser',
            meta: { title: word_is('new') + word_is('user') },
            hidden: true
          },
          {
            path: 'edit/:id',
            component: () => import('@/views/configuration/user/editUser/index.vue'),
            name: 'editUser',
            meta: { title: word_is('edit') + word_is('user') },
            hidden: true
          }
        ]
      },
      {
        path: 'profile',
        component: () => import('@/views/configuration/profile/index'), // Parent router-view
        name: 'ProfileIndex',
        meta: { title: word_is('memory') }
      },
      {
        path: 'global_setting',
        component: () => import('@/views/configuration/global_setting/index'), // Parent router-view
        name: 'Menu1',
        meta: { title: word_is('model') }
      },
      {
        path: 'system',
        component: () => import('@/views/configuration/system/index'), // Parent router-view
        name: 'SystemIndex',
        meta: { title: word_is('system') }
      },
      // userDetail用于跳转到账户信息页面
      {
        path: 'userDetail',
        name: 'UserDetail',
        component: () => import('@/views/configuration/user/userDetail/index'),
        meta: { title: word_is('Account_information') }
      }
    ]
  },

  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// 守卫
router.beforeEach((to, from, next) => {
  window.document.title = to.meta.title
  next()
})

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
